package com.wanqh.reptitle.service.zggw;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.wanqh.reptitle.service.SearchService;
import com.wanqh.reptitle.utils.ExcelUtil;
import com.wanqh.reptitle.utils.SpiderUtils;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.openqa.selenium.WebDriver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service("zggwSearchService")
@Slf4j
public class ZggwSearchServiceImpl implements SearchService {

    @Autowired
    @Qualifier("restTemplate")
    private RestTemplate restTemplate;

    @Autowired
    @Qualifier("firefoxWebDriver")
    private WebDriver firefoxWebDriver;

    @Override
    public String getPlatform() {
        return "中国官网";
    }

    @Override
    public void initExcel() throws Exception {
        ExcelUtil.initExcel(getPlatform(), new String[]{"文件品牌", "搜索匹配结果", "公司名称", "官网地址", "联系方式", "邮箱"}, SpiderUtils.FILE_PATH);
    }

    @Override
    public String[][] ServiceBrand(@NonNull String brand) {
        if (StrUtil.isEmpty(brand)) {
            return null;
        }
        String url = "https://www.maigoo.com/search/?block=all&q=" + brand;

        String[][] result = new String[][]{{brand}};

        firefoxWebDriver.get(url);
        ThreadUtil.sleep(RandomUtil.randomInt(2, 5), TimeUnit.SECONDS);
        Document parse = Jsoup.parse(firefoxWebDriver.getPageSource());
//        c0a0139 font26 category
//        Elements searchResult = parse.getElementsByClass("c0a0139 font26 brand");
        Elements searchResult = parse.getElementsByClass("list");

        String searchText = "";
        String companName = "";
        String gwHref = "";
        String telephoneText = "";
        String emailText = "";
        try {
            if (CollectionUtils.isNotEmpty(searchResult)) {
                Element div = searchResult.get(0);
                Element a = div.child(0);

                String href = a.attr("href");
//             text = element.getElementsByClass("font15 c999").get(0).text().replace("(", "").replace(")", "");
                if (StringUtils.isNotBlank(href)) {
                    firefoxWebDriver.get(href);
                    ThreadUtil.sleep(RandomUtil.randomInt(2, 5), TimeUnit.SECONDS);
                    Document parse2 = Jsoup.parse(firefoxWebDriver.getPageSource());
//                  元素class不确定 companyname font16 line18em dhidden  cname font16
                    Elements companyname_font16_line18em_dhidden_ = parse2.getElementsByClass("companyname font16 line18em dhidden ");
                    Element nameEle = null;
                    if (CollUtil.isNotEmpty(companyname_font16_line18em_dhidden_)) {
                        nameEle = companyname_font16_line18em_dhidden_.get(0);
                    } else {
                        Elements cname_font16 = parse2.getElementsByClass("cname font16");
                        if (CollUtil.isNotEmpty(cname_font16)) {
                            nameEle = cname_font16.get(0);
                        } else {
                            Elements companyname_font16 = parse2.getElementsByClass("companyname font16");
                            if (CollUtil.isNotEmpty(companyname_font16)) {
                                nameEle = companyname_font16.get(0);
                            }
                        }
                    }

                    Elements b_font20_line18em_rline = parse2.getElementsByClass("b font20 line18em rline");
                    if (CollUtil.isNotEmpty(b_font20_line18em_rline)) {
                        searchText = b_font20_line18em_rline.get(0).text();
                    } else {
                        Elements b_font20_line18em_ = parse2.getElementsByClass("b font20 line18em ");
                        if (CollUtil.isNotEmpty(b_font20_line18em_)) {
                            searchText = b_font20_line18em_.get(0).text();
                        }
//                        b font20 line18em
                    }

                    if (nameEle != null) {
                        companName = nameEle.text();
                        // 官网
                        Elements guanwang = parse2.getElementsByClass("iconfont icon-guanwang");
                        if (CollectionUtils.isNotEmpty(guanwang)) {
                            gwHref = guanwang.get(0).siblingElements().get(0).text();
                        }
                        // 电话
                        Elements telephone = parse2.getElementsByClass("iconfont icon-telephone color1");
                        if (CollUtil.isNotEmpty(telephone)) {
                            telephoneText = telephone.get(0).parent().text();
                        }
                        // 邮箱
                        Elements c999_ttl = parse2.getElementsByClass("c999 ttl");
                        if (CollUtil.isNotEmpty(c999_ttl)) {
                            for (Element element : c999_ttl) {
                                if (element.text().equals("邮箱地址：")) {
//                                    邮箱地址：jiaozuozhuoli@jzzl.com
                                    emailText = element.parent().text().replace("邮箱地址：", "");
                                }
                            }
                        }
                    }

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if ((StringUtils.isNotBlank(searchText) && SpiderUtils.isContains(searchText, brand)) ||
                StringUtils.isNotBlank(companName) && SpiderUtils.isContains(companName, brand)) {
            result[0] = new String[]{brand, searchText, companName, gwHref, telephoneText, emailText};
        }
        return result;
    }
}
